package com.atguigu.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 23087
 */
@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CRUD")
@RequestMapping("/pay")
public class PayController {

    @Resource
    private PayService tpayService;

    /**
     * 新增
     * @param pay
     * @return
     */
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    @PostMapping("/add")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int i = tpayService.addNew(pay);
        return ResultData.success("成功插入记录，返回值为：" + i);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Operation(summary = "删除",description = "删除支付流水方法")
    @DeleteMapping("/del/{id}")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        return ResultData.success(tpayService.deleteInfoById(id));
    }

    /**
     * 修改
     * @param payDTO
     * @return
     */
    @Operation(summary = "修改",description = "修改支付流水方法")
    @PutMapping("/update")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay tPay = BeanUtil.toBean(payDTO, Pay.class);
        int i = tpayService.updateInfo(tPay);
        return ResultData.success("成功修改记录，返回值为：" + i);
    }

    /**
     * 根据 id 查询
     * @param id
     * @return
     */
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    @GetMapping("/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){
        return ResultData.success(tpayService.getInfoById(id));
    }

    /**
     * 查询全部
     * @return
     */
    @Operation(summary = "查询所有支付流水",description = "查询所有支付流水方法")
    @GetMapping("/get/all")
    public ResultData<List<Pay>> getAll(){
        return ResultData.success(tpayService.getAllInfos());
    }


    @Value("${server.port}")
    private String port;

    @GetMapping("/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo){

        return "端口号：" + port + "，" + atguiguInfo;
    }



}
